package com.mstore.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mstore.dao.AccountDao;
import com.mstore.pojo.Account;
import com.mstore.util.CalendarUtil;

public class AccountService {

	private AccountDao accountDao;

	public void addAccount(Account account) {
		accountDao.save(account);
	}

	public void updateAccount(Account account) {
		accountDao.update(account);
	}

	public Account findAccount(Account account) {

		return accountDao.findAccount(account);
	}

	public Account selectAccount(Account account) {
		Account a = accountDao.findAccount(account);
		a.setCreateTime(a.getCreateTime().substring(0,
				a.getCreateTime().indexOf(".")));
		return a;
	}

	public void updateAccountMessage(Account account) {
		accountDao.updateAccountMessage(account);
	}

	/**
	 * 验证企业标示符的唯一性
	 */
	public boolean checkParamKey(String paramKey) {
		return accountDao.checkParamKey(paramKey);
	}

	/**
	 * 验证企业名称的唯一性
	 */
	public boolean checkActName(String actName) {
		return accountDao.checkActName(actName);
	}

	/**
	 * 验证企业名称但排除自己
	 */
	public boolean checkActName1(String actName, Long id) {
		return accountDao.checkActName1(actName, id);
	}

	/**
	 * 验证企业邮箱的唯一性
	 */
	public boolean checkActEmail(String email) {
		return accountDao.checkActEmail(email);
	}

	/**
	 * 验证企业手机号的唯一性
	 */
	public boolean checkActMobile(String mobile) {
		return accountDao.checkActMobile(mobile);
	}

	/**
	 * 修改企业密码
	 */
	public void updatePassword(Account account) {
		accountDao.updatePassword(account);
	}
	
	/**
	 * 根据id查询
	 * @param account
	 * @param id
	 * @return
	 */
	public Account FindAccountById(Account account,long id){
		return accountDao.FindAccountById(account, id);
	}
	
	/**
	 * 修改密码
	 * @param account
	 */
	public void UpdatePassword(Account account){
		accountDao.UpdatePassword(account);
	}
	
	/**
	 * 通过标示符查找企业ID
	 */
	public Account selectIdByParamKey(String paramKey) {
		return (Account) accountDao.selectIdByParamKey(paramKey);

	}

	/**
	 * 修改企业登录密码
	 */
	public void updateAccountPassword(Account account) {
		accountDao.updateAccountPassword(account);
	}

	/**
	 * 通过id查询企业信息
	 */
	public Account findAccountById(Account account) {
		return accountDao.findAccountById(account);
	}

	/**
	 * 根据member_id查找
	 */
	public Account findAccountByMemberId(Account account) {
		Object object = accountDao.findAccountByMemberId(account);
		return object != null ? (Account) object : null;
	}

	/**
	 * 查询全部商户
	 */
	public List<Account> tmpGetAll() {
		List<Account> accounts = new ArrayList<Account>();
		List<Object> objects = accountDao.tmpGetAll();
		for (Object object : objects) {
			accounts.add((Account) object);
		}
		return accounts;
	}

	/**
	 * 临时
	 */
	public void tmpUpdate(Account account) {
		accountDao.tmpUpdate(account);
	}

	/**
	 * 对比手机号码查询
	 * 
	 * @return
	 */
	public String getByMobile(Account account) {
		Object object = accountDao.getByMobile(account);
		return String.valueOf(object);
	}

	/**
	 * 通过email修改密码
	 * 
	 * @return
	 * 
	 */
	public void updatePasswordByEmail(Account account) {
		accountDao.updatePasswordByEmail(account);
	}

	/**
	 * 通过memberId修改密码
	 * 
	 * @return
	 */
	public void updatePasswordByMobile(Account account) {
		accountDao.updatePasswordByMobile(account);
	}

	/**
	 * 分页查询全部企业
	 */
	public List<Account> findAllAccountByPage(int index) {
		List<Account> accounts = new ArrayList<Account>();
		List<Object> objects = accountDao.findAllAccountByPage(index);
		for (Object object : objects) {
			accounts.add((Account) object);
		}
		return accounts;
	}

	/**
	 * 查询企业总数
	 */
	public int getAccountCount() {
		return accountDao.getAccountCount();
	}

	/**
	 * 查询商户实时余额总数
	 */
	public float getSumRbalance() {
		return accountDao.getSumRbalance();
	}

	/**
	 * 通过企业名称、手机号模糊查询该企业
	 */
	public List<Account> getAccoutsBySearch(String searchText) {
		List<Account> accounts = new ArrayList<Account>();
		List<Object> objects = accountDao.getAccoutsBySearch(searchText);
		for (Object object : objects) {
			accounts.add((Account) object);
		}
		return accounts;
	}

	/**
	 * 查询实时余额大于0的商户
	 */
	public List<Account> getAccountsHasRbalance() {
		List<Account> accounts = new ArrayList<Account>();
		List<Object> objects = accountDao.getAccountsHasRbalance();
		for (Object object : objects) {
			accounts.add((Account) object);
		}
		return accounts;
	}

	/**
	 * 查询每月入驻商户数
	 */
	public List<Map<String, Object>> getAccountsInMonth() {
		List<Map<String, Object>> accounts = new ArrayList<Map<String, Object>>();
		CalendarUtil calendarUtil = new CalendarUtil();
		Calendar cal = Calendar.getInstance();// 使用日历类
		int year = cal.get(Calendar.YEAR);// 得到年
		int month = cal.get(Calendar.MONTH) + 1;// 得到月，因为从0开始的，所以要加1
		for (int i = 1; i <= month; i++) {
			String[] strings = calendarUtil.getMonthStartAndEnd(i, year);
			int count = accountDao.getAccountsInMonth(year + "-" + strings[0], year + "-" + strings[1]);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put(i + "", (Integer)count);
			accounts.add(map);
		}
		return accounts;
	}
	
	/**
	 * 通过企业名称查询企业
	 */
	public List<Account> searchAccountNameAuto(String searchText) {
		List<Account> accounts = new ArrayList<Account>();
		List<Object> list = accountDao.searchAccountNameAuto(searchText);
		for (Object object : list) {
			accounts.add((Account) object);
		}
		return accounts;
	}

	public AccountDao getAccountDao() {
		return accountDao;
	}

	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}
	
}
